﻿using MediatR;
using T.Materials.API.Application.Command.MaterialStatisticsCommand;
using T.Materials.Repository.MaterialsInterface;

namespace T.Materials.API.Application.CommandHandler.MaterialStatisticsCommandHandler
{
    /// <summary>
    /// 批量删除物资统计命令处理器
    /// </summary>
    public class BatchDeletedMaterialStatisticsCommandHandler : IRequestHandler<BatchDeletedMaterialStatisticsCommand, int>
    {
        /// <summary>
        /// 物资统计仓储接口
        /// </summary>
        protected readonly IMaterialStatisticsRepository materialStatisticsRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="materialStatisticsRepository">物资统计仓储接口</param>
        public BatchDeletedMaterialStatisticsCommandHandler(IMaterialStatisticsRepository materialStatisticsRepository)
        {
            this.materialStatisticsRepository = materialStatisticsRepository;
        }

        /// <summary>
        /// 处理批量删除物资统计命令
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<int> Handle(BatchDeletedMaterialStatisticsCommand request, CancellationToken cancellationToken)
        {
            var ids = request.MaterialStatisticsId;
            foreach(var id in ids)
            {
                var res = await materialStatisticsRepository.GetModelAsync(id);
                res.IsDeleted = true;
                res.DeletedBy = request.DeletedBy;
                res.DeletedDate = DateTime.Now;
                await materialStatisticsRepository.UpdateAsync(res);
            }
            return 1;
        }
    }
}
